package GUILayer;
import ControlLayer.CtrProduct;
import ModelLayer.Product;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;

public class GUIProduct extends javax.swing.JFrame {
    private CtrProduct ctrProduct;

    public GUIProduct() {
        initComponents();
        ctrProduct = CtrProduct.getInstance();
        loadTableContent();
    }

    private void loadTableContent() {
        ((DefaultTableModel)tblProductList.getModel()).setRowCount(0);
        ArrayList<Product> productList = ctrProduct.getProductList();
        for (Product product : productList) {
            String categoryIDs = new String();
            for (Integer categoryID : product.getCategoryIDs()) {
                categoryIDs += categoryID + ", ";
            }
            ((DefaultTableModel)tblProductList.getModel()).addRow(
                new Object [] {
                    product.getIDProduct(),
                    product.getIDSupplier(),
                    product.getIDManufacturer(),
                    product.getIDTax(),
                    product.getIDCategoryDefault(),
                    product.getIDColorDefault(),
                    product.getOnSale(),
                    product.getEan13(),
                    product.getEcotax(),
                    product.getQuantity(),
                    product.getPrice(),
                    product.getWholesalePrice(),
                    product.getReductionPrice(),
                    product.getReductionPercent(),
                    product.getReductionFrom(),
                    product.getReductionTo(),
                    product.getReference(),
                    product.getSupplierReference(),
                    product.getLocation(),
                    product.getWeight(),
                    product.getOutOfStock(),
                    product.getQuantityDiscount(),
                    product.getCustomizable(),
                    product.getUploadableFiles(),
                    product.getTextFields(),
                    product.getActive(),
                    product.getIndexed(),
                    product.getDateAdd(),
                    product.getDateUpd(),
                    product.getDescription(),
                    product.getDescriptionShort(),
                    product.getLinkRewrite(),
                    product.getMetaDescription(),
                    product.getMetaKeywords(),
                    product.getMetaTitle(),
                    product.getName(),
                    product.getAvailableNow(),
                    product.getAvailableLater(),
                    categoryIDs
                }
            );
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrlProductList = new javax.swing.JScrollPane();
        tblProductList = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tblProductList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "id_supplier", "id_manufacturer", "id_tax", "id_category_default", "id_color_default", "on_sale", "ean13", "ecotax", "quantity", "price", "wholesale_price", "reduction_price", "reduction_percent", "reduction_from", "reduction_to", "reference", "supplier_reference", "location", "weight", "out_of_stock", "quantity_discount", "customizable", "uploadable_files", "text_fields", "active", "indexed", "date_add", "date_upd", "description", "description_short", "link_rewrite", "meta_description", "meta_keywords", "meta_title", "name", "available_now", "available_later", "category IDs"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Boolean.class, java.lang.Integer.class, java.lang.Float.class, java.lang.Integer.class, java.lang.Float.class, java.lang.Float.class, java.lang.Float.class, java.lang.Float.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrlProductList.setViewportView(tblProductList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlProductList, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrlProductList, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIProduct().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane scrlProductList;
    private javax.swing.JTable tblProductList;
    // End of variables declaration//GEN-END:variables
}
